import os
from alipay import AliPay
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import serializers

from meiduo_api import settings
from orders.models import OrderInfo
from payments.models import Payment


def get_alipay():
    alipay = AliPay(
        appid=settings.ALIPAY_APPID,
        app_notify_url=None,
        app_private_key_path=os.path.join(settings.BASE_DIR, 'apps/payments/keys/app_private_key.pem'),
        alipay_public_key_path=os.path.join(settings.BASE_DIR, 'apps/payments/keys/alipay_public_key.pem'),
        sign_type="RSA2",
        debug=settings.ALIPAY_DEBUG

    )
    return alipay


class AliapyUrlView(APIView):
    def get(self, request, order_id):
        # 根据订单编号查询对象，获取总金额
        try:
            order = OrderInfo.objects.get(pk=order_id)
        except:
            raise serializers.ValidationError('订单编号无效')
        total_amout = order.total_amount
        print('------------------')
        # 请求支付宝，获取支付链接
        alipay = get_alipay()
        print(alipay)
        order_string = alipay.api_alipay_trade_page_pay(
            out_trade_no=order_id,  # 订单编号
            total_amount=str(total_amout),  # 总金额，转字符串
            subject='美多商城支付' + order_id,
            return_url=settings.RETURN_URL
        )

        alipay_url = settings.ALIPAY_URL + order_string
        return Response({'alipay_url': alipay_url})


class VerifyView(APIView):
    def put(self, request):
        # 获取支付返的参数
        data = request.query_params.dict()
        signature = data.pop("sign")

        order_id = data.get('out_trade_no')
        trade_id = data.get('trade_no')

        # 请求支付宝进行验证
        alipay = get_alipay()
        success = alipay.verify(data, signature)
        if success:
            # 如果验证成功，则保存订单编号对应的支付宝交易编号
            Payment.objects.create(
                order_id=order_id,
                trade_id=trade_id
            )
            # 相应
            return Response({'trade_id': trade_id})
        else:
            raise serializers.ValidationError('支付失败')
